package biznussoft.metercor.fragments;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;

import com.salesforce.androidsdk.rest.RestClient;
import com.salesforce.androidsdk.rest.RestRequest;
import com.salesforce.androidsdk.rest.RestResponse;
import com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback;
import com.salesforce.androidsdk.util.EventsObservable;
import com.salesforce.androidsdk.util.EventsObservable.EventType;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import biznussoft.metercor.HomeActivity;
import biznussoft.metercor.R;
import biznussoft.metercor.listeners.DialogListener;
import biznussoft.metercor.lookup.AccountLookup;
import biznussoft.metercor.lookup.TechicianNameLookup;
import biznussoft.metercor.model.Bean_Class;
import biznussoft.metercor.utils.Base64;
import biznussoft.metercor.utils.Session;

public class NewSurveyFragment extends Fragment implements OnClickListener{
	public static EditText etAccount,etTechnician;
	CheckBox cb1yes,cb1no,cb2yes,cb2no,cb3yes,cb3no,cb4yes,cb4no,cb5yes,cb5no,e1,g1,f1,p1,n1,c1,
	e2,g2,f2,p2,n2,c2,e3,g3,f3,p3,n3,c3,cbInstall,cbExchange;
	String strSignature;
	private Button save;
	public  ImageView imSignatureTecnician;
	private EditText etDate,etPhone;
	public ImageView imSignature;
	DialogListener listener;
	RestClient client;
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		listener = (DialogListener) getActivity();
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.survay, null);
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		client =  HomeActivity.mActivity.client;
		Bean_Class.api_Version = getString(R.string.api_version);
		etAccount = (EditText) getView().findViewById(R.id.etAccountName);
		etTechnician = (EditText) getView().findViewById(R.id.ettechnician);
		imSignatureTecnician = (ImageView)getView().findViewById(R.id.imSignatureWitness);
		imSignature = (ImageView) getView().findViewById(R.id.imSignature);
		etDate = (EditText) getView().findViewById(R.id.etDate);
		save = (Button) getView().findViewById(R.id.saveButton);
		etPhone = (EditText) getView().findViewById(R.id.etphone);
		save.setOnClickListener(this);
		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
		cbInstall = (CheckBox) getView().findViewById(R.id.cbinstall);
		cbExchange = (CheckBox) getView().findViewById(R.id.cbExchange);
		etDate.setText(df.format(c.getTime()));
		etAccount.setOnClickListener(this);
		etTechnician.setOnClickListener(this);
		imSignature.setOnClickListener(this);

		cb1yes = (CheckBox) getView().findViewById(R.id.cb1yes);
		cb1no  = (CheckBox) getView().findViewById(R.id.cb1no);
		cb2yes  = (CheckBox) getView().findViewById(R.id.cb2yes);
		cb2no  = (CheckBox) getView().findViewById(R.id.cb2no);
		cb3yes  = (CheckBox) getView().findViewById(R.id.cb3yes);
		cb3no  = (CheckBox) getView().findViewById(R.id.cb3no);
		cb4yes  = (CheckBox) getView().findViewById(R.id.cb4yes);
		cb4no  = (CheckBox) getView().findViewById(R.id.cb4no);
		cb5yes  = (CheckBox) getView().findViewById(R.id.cb5yes);
		cb5no  = (CheckBox) getView().findViewById(R.id.cb5no);

		e1 = (CheckBox) getView().findViewById(R.id.e1);
		e2 = (CheckBox) getView().findViewById(R.id.e2);
		g1 = (CheckBox) getView().findViewById(R.id.g1);
		g2 = (CheckBox) getView().findViewById(R.id.g2);
		p1 = (CheckBox) getView().findViewById(R.id.p1);
		p2 = (CheckBox) getView().findViewById(R.id.p2);
		n1 = (CheckBox) getView().findViewById(R.id.n1);
		n2 = (CheckBox) getView().findViewById(R.id.n2);
		f1 = (CheckBox) getView().findViewById(R.id.f1);

		f2 = (CheckBox) getView().findViewById(R.id.f2);
		p1 = (CheckBox) getView().findViewById(R.id.p1);
		p2 = (CheckBox) getView().findViewById(R.id.p2);
		c1 = (CheckBox) getView().findViewById(R.id.c1);
		c2 = (CheckBox) getView().findViewById(R.id.c2);

		e3 = (CheckBox) getView().findViewById(R.id.e3);
		g3 = (CheckBox) getView().findViewById(R.id.g3);
		f3 = (CheckBox) getView().findViewById(R.id.f3);
		p3 = (CheckBox) getView().findViewById(R.id.p3);
		c3 = (CheckBox) getView().findViewById(R.id.c3);
		n3 = (CheckBox) getView().findViewById(R.id.n3);



		cb1yes .setOnClickListener(this);
		cb1no .setOnClickListener(this);
		cb2yes .setOnClickListener(this);
		cb2no .setOnClickListener(this);
		cb3yes .setOnClickListener(this);
		cb3no .setOnClickListener(this);
		cb4yes .setOnClickListener(this);
		cb4no .setOnClickListener(this);
		cb5yes .setOnClickListener(this);
		cb5no.setOnClickListener(this);


	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.etAccountName:
			AccountLookup acLookUp = new AccountLookup();
			acLookUp.accountLookupDetails(getActivity(), "survey");
			break;
		case R.id.ettechnician:
			TechicianNameLookup siteworker = new TechicianNameLookup();
			siteworker.getItemLookUp(getActivity(), "survey");
			break;
		case R.id.imSignature:
			listener.nextDialog("sign");
			break;
		case R.id.saveButton:
			insertSurvey();
			break;
		case R.id.cb1yes:
			if(cb1yes.isChecked()){
				cb1no.setChecked(false);
			}
			break;
		case R.id.cb1no:
			if(cb1no.isChecked()){
				cb1yes.setChecked(false);
			}
			break;
		case R.id.cb2yes:
			if(cb2yes.isChecked()){
				cb2no.setChecked(false);
			}
			break;
		case R.id.cb2no:
			if(cb2no.isChecked()){
				cb2yes.setChecked(false);
			}
			break;
		case R.id.cb3yes:
			if(cb3yes.isChecked()){
				cb3no.setChecked(false);
			}
			break;
		case R.id.cb3no:
			if(cb3no.isChecked()){
				cb3yes.setChecked(false);
			}
			break;
		case R.id.cb4yes:
			if(cb4yes.isChecked()){
				cb4no.setChecked(false);
			}
			break;
		case R.id.cb4no:
			if(cb4no.isChecked()){
				cb4yes.setChecked(false);
			}
			break;
		case R.id.cb5yes:
			if(cb5yes.isChecked()){
				cb5no.setChecked(false);
			}
			break;
		case R.id.cb5no:
			if(cb5no.isChecked()){
				cb5yes.setChecked(false);
			}
			break;

		default:
			break;
		}
	}

	public void getSignature(String signature){
		try{
			Log.i("TPT","signature :"+signature);
			strSignature = signature;
			byte[] decodedString = Base64.decode(signature);
			Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
			imSignatureTecnician.setImageBitmap(decodedByte);
		}catch (Exception e) {
		}

	}

	public void insertSurvey(){


		// textView is the TextView view that should display it
		String objectType = "Survey__c";                                                                       
		HashMap<String, Object> fields = new HashMap<String, Object>();
		fields.put("Technician__c", Session.sTechnicianId);
		fields.put("Account__c", Session.sAccountId);
		fields.put("Signature__c","<img src="+"\"data:image/png;base64,"+strSignature+"\"/>");
		fields.put("Q1_No__c", cb1no.isChecked());
		fields.put("Q1_Yes__c", cb1yes.isChecked());
		fields.put("Q2_No__c", cb2no.isChecked());
		fields.put("Q2_Yes__c", cb2yes.isChecked());
		fields.put("Q3_No__c", cb3no.isChecked());
		fields.put("Q3_Yes__c", cb3yes.isChecked());
		fields.put("Q4_No__c", cb4no.isChecked());
		fields.put("Q4_Yes__c", cb4yes.isChecked());
		fields.put("Q5_No__c", cb5no.isChecked());
		fields.put("Q5_Yes__c", cb5yes.isChecked());
		fields.put("C1__c", c1.isChecked());
		fields.put("C2__c", c2.isChecked());
		fields.put("C3__c", c3.isChecked());
		fields.put("E1__c", e1.isChecked());
		fields.put("E2__c", e2.isChecked());
		fields.put("E3__c", e3.isChecked());
		fields.put("F1__c", f1.isChecked());
		fields.put("F2__c", f2.isChecked());
		fields.put("F3__c", f3.isChecked());
		fields.put("G1__c", g1.isChecked());
		fields.put("G2__c", g2.isChecked());
		fields.put("G3__c", g3.isChecked());
		fields.put("NA1__c", n1.isChecked());
		fields.put("NA2__c", n2.isChecked());
		fields.put("NA3__c", n3.isChecked());
		fields.put("P1__c", p1.isChecked());
		fields.put("P2__c", p2.isChecked());
		fields.put("P3__c", p3.isChecked());
		fields.put("Phone__c",etPhone.getText());
		fields.put("Exchange__c", cbExchange.isChecked());
		fields.put("Install__c", cbInstall.isChecked());


		try {
			RestRequest restRequest = RestRequest.getRequestForCreate(Bean_Class.api_Version,objectType,fields);


			client.sendAsync(restRequest, new AsyncRequestCallback() {
				@Override
				public void onSuccess(RestRequest request, RestResponse result) {
					try {   
						Log.i("NCS","Response is  :"+result);
						finishFragment();
					}                                
					catch (Exception e) {
						Log.i("TPT","Exception is   :"+e.getMessage());                      
					}
				}

				@Override
				public void onError(Exception exception) {
					EventsObservable.get().notifyEvent(EventType.RenditionComplete);
				}
			});
		} 
		catch (Exception e) {
			Log.i("NCS","Exception is   :"+e.getMessage());

		}



	}
	public void finishFragment(){
		getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
	}

}
